package com.xunqi.mall.exception;

import com.xunqi.mall.enums.ResponseEnum;
import com.xunqi.mall.vo.ResponseVo;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @Description: 统一异常处理
 * @Created with IntelliJ IDEA.
 * @author: 夏沫止水
 * @create: 2020-04-23 19:00
 **/
@ControllerAdvice
public class RuntimeExceptionHandler {

    /**
     * 全局统一异常处理
     * @param exception
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public ResponseVo handler(RuntimeException exception) {
        return ResponseVo.error(ResponseEnum.ERROR,exception.getMessage());
    }


    /**
     * 用户未登录异常
     * @param exception
     * @return
     */
    @ExceptionHandler(UserLoginException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ResponseVo userHandler(UserLoginException exception) {
        return ResponseVo.error(ResponseEnum.NEED_LOGIN);
    }


    /**
     * 参数非法（效验参数）异常
     * @param exception
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public ResponseVo methodHandler(MethodArgumentNotValidException exception) {
        return ResponseVo.error(ResponseEnum.PARAM_ERROR);
    }
}
